//
//  WYStatusViewModel.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/14.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit


class WYStatusViewModel: NSObject {

    //转发数显示内容
    var retweetStr: String?
    //评论显示内容
    var commentStr: String?
    //赞显示内容
    var unlikeStr: String?
    //处理转发微博内容拼接"@xx:xxx"
    var retweetContent: String?
    //会员等级照片
    var mbrandImage: UIImage?
    //来源(截取字符串)
    var sourceContent: String?
    //认证类型等级图片
    var verifiedImageView: UIImage?
    
       var status: WYStatus?
    init(status: WYStatus) {
        super.init()
        self.status = status
        retweetStr = handleCount(status.reposts_count, defaultTitle: "转发")
        commentStr = handleCount(status.comments_count, defaultTitle: "评论")
        unlikeStr = handleCount(status.attitudes_count, defaultTitle: "赞")
        
        //处理会员图片
        handleMBrankImage(status.user?.mbrank ?? 0)
        //处理来源字符串
        handleSourceContent(status.source ?? "")
        //认证等级
        handleVerifiedtypeImage(status.user?.verified_type ?? 0)
        
        print(status.source)
    }
    //处理转发微博内容显示 
    private func handleReteetContent(status: WYStatus) {
        
        if status.retweeted_status != nil {
        
            guard let name = status.retweeted_status?.user?.screen_name , let text = status.retweeted_status?.text else {
            
                return
            }
            //d代码执行到此一定有内容
            retweetContent = "@\(name):\(text)"
        }
        
    }
    
    
    var timeStr: String? {
        //  判断发布微博时间是否为nil
        guard let createAtStr = self.status?.created_at else {
            
            return nil
        }
        
        return NSDate.sinaDate(createAtStr).sincedateString
    }
    //处理 转发...的数据
    /*! 如果大于0 
            判断 count 是否大于等于10000
                对10000这样的数据进行格式化处理
            否则  直接显示应有的数字
        否则显默认标题
 
    */
    private func handleCount(count: Int, defaultTitle: String) -> String {
    
        if count > 0 {
        
            if count >= 10000 {
            
                let result = Float(count/1000) / 10
                var resultStr = "\(result)万"
                
                if resultStr.containsString("0") {
                    
                    resultStr = resultStr.stringByReplacingOccurrencesOfString("0", withString:"")
                    
                }
                return resultStr
            } else {
            
                return "\(count)"
            }
        } else {
        
            return defaultTitle
        }
    }
    //处理来源字符串
    private func handleSourceContent(sourcr: String) {
    
        //'\'进行字符创的转译
        guard let range1 = sourcr.rangeOfString("\">"), let range2 = sourcr.rangeOfString("</") else {
        
            return
        }
        //代码执行到此 范围一定存在
        //开始光标位置
        let startIndex = range1.endIndex
        //光标结束为止
        let endIndex = range2.startIndex
        //截取字符串
        sourceContent = "来自 " + sourcr.substringWithRange(startIndex..<endIndex)
    }
    //处理会员等级图片
    private func handleMBrankImage(mbrank: Int) {
    
        if mbrank >= 0 && mbrank <= 6 {
        
            let  image = UIImage(named: "common_icon_membership_level\(mbrank)")
            mbrandImage = image
        }
    }
    //处理认证等级图片
    private func handleVerifiedtypeImage(verifiedtype: Int) {
    
        //认证类型等级 认证类型 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
        switch verifiedtype {
        
        case 0:
            verifiedImageView = UIImage(named: "avatar_vip")
        case 2,3,4:
            verifiedImageView = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImageView = UIImage(named: "avatar_grassroot")
        default:
            verifiedImageView = nil
            
        }
    }
    
 
}
